//
//  RegisterViewController.m
//  WeCommunity
//
//  Created by mac on 2017/6/20.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "RegisterViewController.h"
#import "UserServer.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController

@synthesize loginView = _loginView;

-(CustomLoginView *)loginView
{
    if (!_loginView) {
        _loginView = [[CustomLoginView alloc] initWithFrame:self.view.frame withType:Type_Register];
        _loginView.delegate = self;
    }
    return _loginView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = WHITE_COLOR;
    
    // 如果自定义返回按钮后, 滑动返回可能失效, 需要添加下面的代码
    __weak typeof(self)Weakself = self;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)Weakself;
}

-(void)LoginViewProcessBtn:(NSInteger)btnIndex
{
    switch (btnIndex) {
        case 0:
        {
            [self submitRegister];
        }
            break;
        case 1:
        {
            [self returnBack];
        }
            break;
            
        default:
            break;
    }
}
#pragma mark - 提交注册
-(void)submitRegister
{
    [self showLoadingHudWithHText:nil];
    
    ContactModel * userContact = [ContactModel new];
    userContact.userName = self.loginView.username;
    userContact.password = self.loginView.password;
    //暂时先不MD5加密
    __weak typeof(self) weakSelf = self;
    [[UserServer shared] registerUser:userContact complete:^(BOOL succees, NSString *error, ContactModel *contact) {
        [self hiddenLoadingHud];
        if (succees) {
            [weakSelf sureAlertWithTitle:nil andMesg:KNSLocal(@"registerSuccess")];
            [weakSelf returnBack];
        }else{
            [weakSelf sureAlertWithTitle:nil andMesg:error];
        }
    }];
}

#pragma mark - 返回
-(void)returnBack
{
    [self.loginView hiddenKeyBoard];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 取消
-(void)cancel
{
    [self returnBack];
}

@end
